<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Question;
use App\Admin\Subject;
use App\Admin\Sections;
use App\Http\Controllers\Controller;
use App\Http\Requests\QuestionRequest;
use App\Repositories\QuestionRepositories;
//use http\Env\Request;

class QuestionController extends Controller
{
    protected $repo;

    public function __construct(QuestionRepositories $repo)
    {
        $this->repo = $repo;
    }

    public function index()
    {
        $data = Question::with('types', 'authors','subjects')->paginate(10);

        return view('admin.question', compact('data'));
    }

    public function create()
    {
        $data = Subject::get();
        $sections=Sections::all();
        return view('admin.question_add', compact('data','sections'));
    }

    public function show(Subject $id)
    {
        $data = Question::with('types', 'authors','subjects')->where('subject',$id->id)->paginate(10);
        return view('admin.question', compact('data'));
    }
    public function sectionsshow(Sections $id)
    {
        $data = Question::with('types', 'authors','subjects')->where('sections',$id->id)->paginate(10);
        return view('admin.question', compact('data'));
    }

    public function store(QuestionRequest $request)
    {
        return $this->repo->save($request) ? '1' : '0';
    }

    public function edit(Question $question)
    {
        $question = $this->repo->edit($question);
        $subject = Subject::where('id',$question->subject)->get();
        return view('admin.question_edit', compact('question','subject'));
    }

    public function update(Question $question, QuestionRequest $request)
    {
        $data = $this->repo->editdata($request);
        return $question->update($data) ? '1' : '0';
    }

    public function destroy(Question $question)
    {
        return $question->delete() ? '1' : '0';
    }

}
